package hashmap;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 给你一个整数数组 nums ，判断是否存在三元组 [nums[i],
 * nums[j], nums[k]] 满足 i != j、i != k 且 j != k ，
 * 同时还满足 nums[i] + nums[j] + nums[k] == 0 。请
 * 你返回所有和为 0 且不重复的三元组。
 *
 * 注意：答案中不可以包含重复的三元组。
 */
public class LeetCode_15 {
    public int numJewelsInStones(String jewels, String stones) {
        int[] map = new int[58];
        int sum = 0;
        for (int i = 0; i < jewels.length(); i++) {
            char j = jewels.charAt(i);
            map[j - 65] = 1;
        }
        for (int i = 0; i < stones.length(); i++) {
            char s = stones.charAt(i);
            if(map[s - 65] == 1){
                sum++;
            }
        }
        return sum;
    }
    public static void main(String[] args) {

//        LeetCode_15 leetCode15 = new LeetCode_15();
//        List<List<Integer>> lists = leetCode15.threeSum(new int[]{-2,0,0,2,2});
//        lists.stream().forEach(
//                v -> {
//                    System.out.println(v);
//                }
//        );
    }
    public List<List<Integer>> threeSum(int[] nums) {
        ArrayList<List<Integer>> resultList = new ArrayList<>();
        Arrays.sort(nums);
        int left = 0;
        int right = nums.length - 1;
        int mid = left + 1;
        while (left < right - 1){
            if((mid >= right) || (left > 0 && nums[left - 1] == nums[left])){
                right = nums.length - 1;
                left++;
                mid = left + 1;
                continue;
            }
            int result = nums[left] + nums[mid] + nums[right];
            if(result > 0){
                right--;
            }
            if(result < 0){
                mid++;
            }
            if(result == 0){
                ArrayList<Integer> re = new ArrayList<>();
                re.add(nums[left]);
                re.add(nums[mid]);
                re.add(nums[right]);
                resultList.add(re);
                do{
                    mid++;
                }while (mid < right && nums[mid] == nums[mid - 1]);
                do{
                    right--;
                }while (right > mid && nums[right + 1] == nums[right]);
            }
        }
        return resultList;
    }
}
